﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DotNetExtensions.AspNet.IOC
{
    public class BuilderOption
    {
        public BuilderOption()
        {
            Assemblies = Assemblies.Default;
            Filters = new List<InjectTypeFilter>();
            Pattern = new List<string>();
        }
        public Assemblies Assemblies { get; private set; }

        public List<InjectTypeFilter> Filters { get; private set; }
        public List<string> Pattern { get; set; }
    }

    public class Assemblies : Singleton<Assemblies>
    {
        private List<Assembly> assemblies;
        private Assemblies()
        {
            assemblies = new List<Assembly>();
        }

        public Assembly this[int index]
        {
            get { return assemblies[index]; }
        }

        public Assembly this[string fullname]
        {
            get { return assemblies.FirstOrDefault(a => a.FullName == fullname); }
        }

        public List<Assembly> All { get => assemblies; }

        public void Add(Assembly assembly)
        {
            if (!assemblies.Any(a => a.FullName == assembly.FullName))
            {
                assemblies.Add(assembly);
            }
        }

        public void AddRange(IEnumerable<Assembly> assemblys)
        {
            foreach (var item in assemblys) Add(item);
        }

        public bool Any() => assemblies.Any();

        public IEnumerable<Type> SelectMany(Func<Assembly, IEnumerable<Type>> selector)
        {
            return assemblies.SelectMany(selector).Distinct();
        }
    }

    public class InjectTypeFilter
    {
        public Type Type { get; set; }
        public InjectKind Kind { get; set; }
    }

    public enum InjectKind
    {
        Named = 0,
        Typed = 1
    }
}
